package com.lincoln.zuul;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 请求参数合法性检验拦截器
 */
@Component
public class ParamsFilter extends ZuulFilter {

    @Value("{API_CHECKSUM_KEY}")
    public static String checkSumKey = "zldkP!920-1_+}|?.2,cip23TYn2xj^&";

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
//        String checksum = request.getParameter("checksum");
//        if (checksum == null || checksum.equals("")) {
//            throw new RouterException();
//        }
//        Map<String, String[]> paramsMap = request.getParameterMap();
//        String encodeCheckSum = WebParamUtils.getCheckSum(paramsMap, checkSumKey);
//        if (!encodeCheckSum.toLowerCase().equals(checksum.toLowerCase())) {
//            throw new RouterException();
//        }
//        //将checksum从params中删除
//        request.getParameterMap().remove("checksum");
        if (request.getHeader("authorization") != null) {
            String token = request.getHeader("authorization");
            ctx.addZuulRequestHeader("authorization", token);
        }

        //判断是否是文件上传
        return null;
    }
}
